package com.atguigu.spzx.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;


import java.time.Duration;

/**
 * @author LT
 * @create 2024-03-25-19:53
 */

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class,args);
    }

    //注入配置跨域的filter对象
    @Bean
    public CorsWebFilter corsWebFilter(){
        //基于路径配置跨域
        UrlBasedCorsConfigurationSource configurationSource =
                new UrlBasedCorsConfigurationSource();

        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedMethod("*");
        config.addAllowedOriginPattern("*");
        config.addAllowedHeader("*");
        //30分钟内不在检查是否允许跨域访问
        config.setMaxAge(Duration.ofMinutes(30L));
        //配置 /** 所有的请求访问时，使用config对象的配置允许跨域
        configurationSource.registerCorsConfiguration("/**" , config);

        return new CorsWebFilter(configurationSource);
    }







}


